package com.skyable.third.utils;

import com.alibaba.fastjson.JSONObject;
import com.skyable.third.feign.SmsFeignApi;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

/**
 * @author GaoYuan
 * @title: SmsUtil
 * @projectName spring-cloud-oauth2-demo
 * @description: TODO
 * @date 2021/4/1314:01
 */
@Slf4j
@Configuration
public class SmsUtil {

    @Value("${sms.server.sid}")
    private String SMS_SERVER_SID;
    @Value("${sms.server.token}")
    private String SMS_SERVER_TOKEN;
    @Value("${sms.server.appid}")
    private String SMS_SERVER_APPID;
    @Value("${sms.server.template_code}")
    private String SMS_SERVER_TEMPLATE_CODE;
    @Value("${sms.server.template_pass}")
    private String SMS_SERVER_TEMPLATE_PASS;
    @Value("${sms.server.template_deny}")
    private String SMS_SERVER_TEMPLATE_DENY;

    @Autowired
    private SmsFeignApi smsFeignApi;

    /**
     * 发送验证码
     * @param phone
     * @param param
     * @return
     */
    public JSONObject sendCode(String phone, String param) {
        // 封装参数，千万不要替换为Map与HashMap，否则参数无法传递
        Map<String, String> params = new HashMap<>();
        params = addCommonParam(params);
        params.put("templateid",SMS_SERVER_TEMPLATE_CODE);
        params.put("param",param);
        params.put("mobile",phone);
        return smsFeignApi.sendsms(params);
    }

    /**
     * 发送用户登录密码
     * @param phone
     * @param param
     * @return
     */
    public JSONObject sendPass(String phone, String param) {
        // 封装参数，千万不要替换为Map与HashMap，否则参数无法传递
        Map<String, String> params = new HashMap<>();
        params = addCommonParam(params);
        params.put("templateid",SMS_SERVER_TEMPLATE_PASS);
        params.put("param",param);
        params.put("mobile",phone);
        return smsFeignApi.sendsms(params);
    }

    /**
     * 发送用户申请失败原因
     * @param phone
     * @param param
     * @return
     */
    public JSONObject sendFail(String phone, String param) {
        // 封装参数，千万不要替换为Map与HashMap，否则参数无法传递
        Map<String, String> params = new HashMap<>();
        params = addCommonParam(params);
        params.put("templateid",SMS_SERVER_TEMPLATE_DENY);
        params.put("param",param);
        params.put("mobile",phone);
        return smsFeignApi.sendsms(params);
    }

    private Map<String,String> addCommonParam(Map<String, String> params) {
        params.put("sid",SMS_SERVER_SID);
        params.put("token",SMS_SERVER_TOKEN);
        params.put("appid",SMS_SERVER_APPID);
        return params;
    }
}
